/*******************************************************************************
 * Copyright (c) 2008 Sylvain Sicard.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Sylvain Sicard - initial API and implementation
 ******************************************************************************/
package fr.jade.resources.sgbd;

import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.UnknownHostException;

import org.hsqldb.Server;
import org.objectweb.fractal.api.NoSuchInterfaceException;
import org.objectweb.fractal.api.control.IllegalBindingException;
import org.objectweb.fractal.api.control.IllegalLifeCycleException;
import org.objectweb.fractal.api.control.LifeCycleController;

import fr.jade.base.JadeBaseComponent;
import fr.jade.fraclite.api.control.NoSuchAttributeException;
import fr.jade.resources.api.DataBase;
import fr.jade.resources.api.JdbcConnector;

public class HsqlDbWrapper extends JadeBaseComponent implements LifeCycleController, JdbcConnector{

  // Attributes 
  protected static final String portAttName = "port";
  protected int port;
  
  protected static final String[] attList = {portAttName};
  
  // Bindings
  protected static final String dbItfName = "db";
  protected DataBase db;
  
  protected static final String[] itfList = {dbItfName};
  
  //
  Server server;
  
  boolean started = false;
  
  public HsqlDbWrapper(){
    server = new Server();
    server.setErrWriter(new PrintWriter(System.err));
    server.setLogWriter(new PrintWriter(System.out));
  }
  
  // JdbcConnector
  public String getURL(){
    String host=null;
    try {
      host = InetAddress.getLocalHost().getHostName();
    } catch (UnknownHostException e) {
      e.printStackTrace();
    }
    return "jdbc:hsqldb:hsql://"+host+":"+port+"/"+db.getDataBaseName();
  }
  // LifeCycleController
  @Override
  public String getFcState() {
    return started?STARTED:STOPPED;
  }

  @Override
  public void startFc() throws IllegalLifeCycleException {
    if(!started){
      log("Start HsqlDb server");
      server.setPort(port);
      server.setDatabaseName(0, db.getDataBaseName());
      server.setDatabasePath(0, db.getDataBasePath());
      
      server.start();
      started=true;
    }
  }

  @Override
  public void stopFc() throws IllegalLifeCycleException {
    if(started){
      log("Stop HsqlDb server");
      server.stop();
      started=false;
    }
  }

  // BindingController
  @Override
  public void bindFc(String itfName, Object itfValue) throws NoSuchInterfaceException, IllegalBindingException, IllegalLifeCycleException {
    if(itfName.equals(dbItfName)){
      db = (DataBase)itfValue;
    }else{
      super.bindFc(itfName, itfValue);
    }
  }

  @Override
  public void unbindFc(String itfName) throws NoSuchInterfaceException, IllegalBindingException, IllegalLifeCycleException {
    if(itfName.equals(dbItfName)){
      db = null;
    }else{
      super.unbindFc(itfName);
    }
  }

  @Override
  public Object lookupFc(String itfName) throws NoSuchInterfaceException {
    if(itfName.equals(dbItfName)){
      return db;
    }else{
      return super.lookupFc(itfName);
    }
  }
  
  @Override
  public String[] listFc() {
    return super.concat(itfList, super.listFc());
  }

  // GenericAttributeController
  @Override
  public void setAttribute(String name, String value) throws NoSuchAttributeException {
    if(name.equals(portAttName)){
      port = Integer.parseInt(value);
    }else{
      super.setAttribute(name, value);
    }
  }

  @Override
  public String getAttribute(String name) throws NoSuchAttributeException {
    if(name.equals(portAttName)){
      return Integer.toString(port);
    }else{
      return super.getAttribute(name);
    }
  }

  @Override
  public String[] listFcAtt() {
    return super.concat(attList, super.listFcAtt());
  }
}
